﻿using DotSpatial.Controls;
using DotSpatial.Symbology;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DotSpatialExcelAddIn
{
    public partial class frmMap : Form
    {
        bool dropLegendChk = true;
        addinRibbon rb;
        public addinRibbon Rb
        {
            get { return rb; }
            set { rb = value; }
        }
        int frmHeight = 0;
        //----------------------Interface Map for Ribbon---------------------------
        IMap XLSMap;
        public IMap mapInterface
        {
            get { return map1; }
            set { XLSMap = (Map)value; }
        }
        //-------------------------------------------------------------------------
        
        public frmMap()
        {
            InitializeComponent();
            //map1.Layers.Add(BruTileLayer.CreateOsmLayer());
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (this.Height == 88)
            {
                this.Height = frmHeight;
            }
            else
            {
                frmHeight = this.Height;
                this.Height = 88;
            }
            /*
            Microsoft.Office.Interop.Excel.Application oXL;
            Microsoft.Office.Interop.Excel.Workbook oWB;
            Microsoft.Office.Interop.Excel.Worksheet oSheet;
            oXL = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
            oXL.Visible = true;
            oWB = (Microsoft.Office.Interop.Excel.Workbook)oXL.ActiveWorkbook;
            MessageBox.Show(oWB.Worksheets.Count.ToString());
            for (int i = oWB.Worksheets.Count; i>= 1; i--)
            {
                oSheet = (Worksheet)oWB.Worksheets[i];
                oSheet.Delete(); 
            }
            oWB.Worksheets.Add();
            oSheet = (Worksheet)oWB.Worksheets[1];
            
            //oSheet = (Worksheet)oWB.Worksheets["Sheet1"];
            oSheet.Name = "Kasem";
             */
        }

        private void frmMap_Load(object sender, EventArgs e)
        {
            //-----------------
            rb.enableTool = true;
            //-----------------
        }
        
        private void frmMap_FormClosed(object sender, FormClosedEventArgs e)
        {
            rb.enableTool = false;
        }

        private void spatialStatusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
        }

#region "legend"
       
        private void legend1_MouseHover(object sender, EventArgs e)
        {
            legend1.Height = map1.Top + map1.Height;    
        }

        private void legend1_MouseLeave(object sender, EventArgs e)
        {
            if (dropLegendChk==true) legend1.Height = 20;    
        }

        private void picDropLegend_Click(object sender, EventArgs e)
        {
                legend1.Height = map1.Top + map1.Height;
                dropLegendChk = false;
                picLegendUp.Visible = true;
                picDropLegend.Visible = false;
        }

        private void picLegendUp_Click(object sender, EventArgs e)
        {
                legend1.Height = 20;
                dropLegendChk = true;
                picDropLegend.Visible = true;
        }
  
#endregion

        private void opacityBar_Scroll(object sender, EventArgs e)
        {
            this.Opacity = opacityBar.Value * 0.01;
           //this.Refresh(); 
        }

        private void cmdOpenOnlinemap_Click(object sender, EventArgs e)
        {
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = map1.Layers.Count - 1; i >= 0; i--)
            {
                string str = map1.Layers[i].LegendText;
                string str2 = str.Substring(0, 11);
                if (str2.ToUpper() == "Brutile.Web".ToUpper())
                {
                    map1.Layers.RemoveAt(i);
                }
            }

            if (comboBox1.Text == "OSM") map1.Layers.Add(BruTileLayer.CreateOsmLayer());
            if (comboBox1.Text == "Bring map") map1.Layers.Add(BruTileLayer.CreateBingHybridLayer());
            if (comboBox1.Text == "Yahoo map") map1.Layers.Add(BruTileLayer.CreateGoogleSatelliteLayer());
            for (int i = map1.Layers.Count - 1; i >= 0; i--)
            {
                string str = map1.Layers[i].LegendText;
                string str2 = str.Substring(0, 11);
                if (str2.ToUpper() == "Brutile.Web".ToUpper())
                {
                    IMapLayer iMapLayer = map1.Layers[i];
                    for (int j = i; j > 0; j--)
                    {
                        map1.Layers[j] = map1.Layers[j - 1];
                    }
                    map1.Layers[0] = iMapLayer;
                }
            }
        }

        private void frmMap_Activated(object sender, EventArgs e)
        {
            rb.enableTool = true;
        }



    }
}
